package com.ethink.platform.controller.warehouse;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.framework.common.web.BaseController;
import com.ethink.framework.common.web.context.SPRequestContextHolder;
import com.ethink.framework.common.web.response.CommonResponse;
import com.ethink.platform.controller.order.info.ExportInfo;
import com.ethink.platform.manager.ExportManager;
import com.ethink.platform.manager.LogisticsTraceOrderManager;
import com.ethink.platform.manager.LogisticsTransferOrderManager;
import com.ethink.warehouse.api.LogisticsTransferOrderFacade;
import com.ethink.warehouse.api.model.LogisticsTransferOrderExcelModel;
import com.ethink.warehouse.api.model.LogisticsTransferOrderReturnExcelModel;
import com.ethink.warehouse.api.request.*;
import com.ethink.warehouse.api.response.LogisticsTransferOrderAddResponse;
import com.ethink.warehouse.api.response.LogisticsTransferOrderDetailResponse;
import com.ethink.warehouse.api.response.LogisticsTransferOrderQueryItemModeResponse;
import com.ethink.warehouse.api.response.LogisticsTransferOrderQueryResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 物流调拨单 Controller
 *
 * @author default
 * @date 2022-11-10
 */
@Tag(name = "LogisticsTransferOrderController", description = "物流调拨单 Controller")
@RestController
@Slf4j
@RequestMapping("platform-server/v1/warehouse-service")
public class LogisticsTransferOrderController extends BaseController {

    @Autowired
    private LogisticsTransferOrderFacade logisticsTransferOrderFacade;

    @Autowired
    private LogisticsTransferOrderManager logisticsTransferOrderManager;

    @Autowired
    private ExportManager exportManager;

    @Operation(summary = "分页查询")
    @GetMapping("/logisticsTransferOrder/query")
    public CommonResponse<List<LogisticsTransferOrderQueryResponse>> queryLogisticsTransferOrder(LogisticsTransferOrderQueryRequest request) {
        CommonResponse<List<LogisticsTransferOrderQueryResponse>> response = succeedWithPage(logisticsTransferOrderFacade.queryLogisticsTransferOrder(request));
        response.setExtension(logisticsTransferOrderFacade.queryLogisticsTransferOrderSum(request));
        return response;
    }

    @Operation(summary = "物流调拨单导出Excel")
    @GetMapping("/logisticsTransferOrder/export")
    public CommonResponse<ExportInfo> exportLogisticsTransferOrder(LogisticsTransferOrderQueryRequest request) {
        return succeedWith(exportManager.exportLogisticsTransferOrder(request));
    }

    @Operation(summary = "货品模式分页查询")
    @GetMapping("/logisticsTransferOrder/pageQueryItemMode")
    public CommonResponse<List<LogisticsTransferOrderQueryItemModeResponse>> queryItemModeLogisticsTransferOrder(LogisticsTransferOrderQueryItemModeRequest request) {
        Page<LogisticsTransferOrderQueryItemModeResponse> logisticsTransferOrderQueryItemModeResponsePage = logisticsTransferOrderFacade.queryItemModeLogisticsTransferOrder(request);
        return succeedWithPage(logisticsTransferOrderQueryItemModeResponsePage);
    }

    @Operation(summary = "物流调拨单货品模式导出Excel")
    @GetMapping("/logisticsTransferOrder/exportGoodsMode")
    public CommonResponse<ExportInfo> exportLogisticsTransferOrderGoodsMode(LogisticsTransferOrderQueryItemModeRequest request) {
        return succeedWith(exportManager.exportLogisticsTransferOrderGoodsMode(request));
    }

    @Operation(summary = "详情查询")
    @GetMapping("/logisticsTransferOrder/get/{logisticsTransferOrderId}")
    public CommonResponse<LogisticsTransferOrderDetailResponse> getLogisticsTransferOrderById(@PathVariable("logisticsTransferOrderId") Long logisticsTransferOrderId) {
        return succeedWith(logisticsTransferOrderFacade.getLogisticsTransferOrderById(logisticsTransferOrderId));
    }

    @Operation(summary = "导入")
    @PostMapping("/logisticsTransferOrder/import")
    public CommonResponse<List<LogisticsTransferOrderReturnExcelModel>> logisticsTransferOrderImport(@RequestParam("file") MultipartFile file
            , @RequestParam("subWarehouseCode") String subWarehouseCode) {
        if (StrUtil.isBlank(subWarehouseCode)) {
            throw new BusinessException(CommonRespCode.PARAM_ERR, "调出仓不可以为空");
        }
        return succeedWith(logisticsTransferOrderManager.logisticsTransferOrderImport(file, subWarehouseCode));
    }

    @Operation(summary = "新增")
    @PostMapping("/logisticsTransferOrder/add")
    public CommonResponse<LogisticsTransferOrderAddResponse> addLogisticsTransferOrder(@RequestBody @Validated LogisticsTransferOrderAddRequest request) {
        request.setCreateUserCode(SPRequestContextHolder.getCurrentSystemUserLoginName());
        request.setCreateUserName(SPRequestContextHolder.getCurrentSystemUserRealName());
        return succeedWith(logisticsTransferOrderFacade.addLogisticsTransferOrder(request));
    }

    @Operation(summary = "更新")
    @PutMapping("/logisticsTransferOrder/update")
    public CommonResponse updateLogisticsTransferOrder(@RequestBody @Validated LogisticsTransferOrderUpdateRequest request) {
        logisticsTransferOrderFacade.updateLogisticsTransferOrder(request);
        return succeed();
    }

    @Operation(summary = "删除")
    @PutMapping("/logisticsTransferOrder/delete/{logisticsTransferOrderId}")
    public CommonResponse deleteLogisticsTransferOrder(@PathVariable("logisticsTransferOrderId") Long logisticsTransferOrderId) {
        logisticsTransferOrderFacade.deleteLogisticsTransferOrder(logisticsTransferOrderId);
        return succeed();
    }

    @Operation(summary = "更新状态")
    @PutMapping("/logisticsTransferOrder/setStatus")
    public CommonResponse setLogisticsTransferOrderStatus(@RequestBody @Validated LogisticsTransferOrderSetStatusRequest request) {
        logisticsTransferOrderFacade.setLogisticsTransferOrderStatus(request);
        return succeed();
    }

    @Operation(summary = "撤回")
    @PutMapping("/logisticsTransferOrder/cancel/{logisticsTransferOrderId}")
    public CommonResponse cancelLogisticsTransferOrder(@PathVariable("logisticsTransferOrderId") Long logisticsTransferOrderId) {
        logisticsTransferOrderFacade.cancelLogisticsTransferOrder(logisticsTransferOrderId);
        return succeed();
    }
}